package com.xuliugen.springboot.service;

import com.xuliugen.springboot.domain.User;
import com.xuliugen.springboot.mapper.UserMapper;
import com.xuliugen.springboot.property.PropertyEnum;
import com.xuliugen.springboot.util.WebUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by xuliugen on 2017/3/26.
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public int register(String username, String password) {
        return userMapper.insert(UUID.randomUUID().toString().replace("-", "").toUpperCase(), username, password);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer addBlance(String username, Integer blance) throws Exception {
        String respContent = WebUtils.httpPostWithJSON(PropertyEnum.API_SLW_URL.getValue(), getMap(username, blance));
        System.out.println(respContent);
        return null;
    }

    @Override
    public List<User> listUser() {
        return userMapper.listUsers();
    }

    private Object getMap(String userName,Integer blance) {
        Map map=new HashMap<>(16);
        User user=new User();
        user.setBlance(blance);
        user.setUsername(userName);
        map.put("user",user);
        return map;
    }
}
